package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * SpringMvc提供的全局异常处理机制
 * @ControllerAdvice(annotations = {Controller.class,RestController.class})     表示该类为一个异常处理类
 * @ResponseBody 该类返回的数据全部都会转换为json字符串
 */

@ControllerAdvice(annotations = {Controller.class,RestController.class})
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)   //代表了该方法会处理的所有异常,只要是controller出现出现异常,都会被处理
    public R<String> handleException(Exception exception){
    exception.printStackTrace();
    return R.error("服务器异常:"+exception.getMessage());

}


}
